﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dapper;
using LibMS.Models;
using System.Web;

namespace LibMS.Controllers
{
    [AllowAnonymous]
    public class ApiLibBorrowDocumentController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetData(int index, int size, string order)
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@PageSize", size);
            parameters.Add("@PageIndex", index);
            parameters.Add("@OrderBy", order);
            var data = _unitOfWork.LibBorrowDocument.GetData(out total, out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var sqlResult = data as List<LibBorrowDocument> ?? null;
            var result = new
            {
                success = true,
                message = string.Empty,
                total = total,
                data = sqlResult
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetById(int documentId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@DocumentId", documentId);
            var data = _unitOfWork.LibBorrowDocument.GetById(out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = data
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        //[Authorize(Roles = "System Admin, Lib Admin")]
        //[System.Web.Http.HttpPost]
        //public HttpResponseMessage UpdateData(LibBorrowDocument entity)
        //{
        //    string message;
        //    var parameters = new DynamicParameters();
        //    parameters.Add("@DocumentId", entity.DocumentId);
        //    parameters.Add("@CategoryId", entity.CategoryId);
        //    parameters.Add("@RackId", entity.RackId);
        //    parameters.Add("@KyHieu", entity.KyHieu);
        //    parameters.Add("@DocumentName", entity.DocumentName);
        //    parameters.Add("@ThumbnaiPath", entity.ThumbnaiPath);
        //    parameters.Add("@NXB", entity.NXB);
        //    parameters.Add("@Author", entity.Author);
        //    parameters.Add("@Total", entity.Total);
        //    parameters.Add("@Descriptions", entity.Descriptions);
        //    var data = _unitOfWork.LibBorrowDocument.Update(out message, parameters);
        //    if (data == null)//Query error
        //    {
        //        var error = new
        //        {
        //            success = false,
        //            message = message
        //        };
        //        return Request.CreateResponse(HttpStatusCode.OK, error);
        //    }
        //    //Success
        //    var result = new
        //    {
        //        success = true,
        //        message = string.Empty,
        //        data = entity
        //    };
        //    return Request.CreateResponse(HttpStatusCode.OK, result);
        //}

        [Authorize(Roles = "System Admin, Lib Admin")]
        [System.Web.Http.HttpPost]
        public HttpResponseMessage CreateData(LibBorrowDocument entity)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@UserId", entity.UserId);
            parameters.Add("@DocumentId", entity.DocumentId);
            parameters.Add("@BorrowDate", entity.BorrowDate);
            parameters.Add("@BorrowDayCount", entity.BorrowDayCount);
            parameters.Add("@Total", entity.Total);
            parameters.Add("@Reason", entity.Reason);
            parameters.Add("@BorrowDocumentId", dbType: System.Data.DbType.Int32, direction: System.Data.ParameterDirection.Output);
            var data = _unitOfWork.LibBorrowDocument.Create(out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var borrowDocumentId = parameters.Get<int>("@BorrowDocumentId");
            entity.BorrowDocumentId = borrowDocumentId;
            var result = new
            {
                success = true,
                message = string.Empty,
                data = entity
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [Authorize(Roles = "System Admin, Lib Admin")]
        [System.Web.Http.HttpPost]
        public HttpResponseMessage ReturnDocument(int borrowDocumentId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@BorrowDocumentId", borrowDocumentId);
            parameters.Add("@ReturnStatus", dbType: System.Data.DbType.Int32, direction: System.Data.ParameterDirection.Output);
            parameters.Add("@CurrentBorrowDate", dbType: System.Data.DbType.Date, direction: System.Data.ParameterDirection.Output);
            parameters.Add("@CurrentReturnDate", dbType: System.Data.DbType.Date, direction: System.Data.ParameterDirection.Output);
            var data = _unitOfWork.LibBorrowDocument.ExecuteSp(out message, StringConstant.TABLE_LIBBORROWDOCUMENT + StringConstant.SP_LIBBORROWDOCUMENT_RETURNDOCUMENT, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var returnStatus = parameters.Get<int>("@ReturnStatus");
            var currentBorrowDate = parameters.Get<DateTime>("@CurrentBorrowDate");
            var currentReturnDate = parameters.Get<DateTime>("@CurrentReturnDate");
            var result = new
            {
                success = true,
                message = string.Empty,
                returnstatus = returnStatus,
                borrowdate = currentBorrowDate,
                returndate = currentReturnDate
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}
